<?php
class service_menu{
    static public function getAll(){
        $menu = new DaoMenu();
        $menus = array();
        $menus = $menu->getAll("1 order by sortID ASC");
        return $menus;
    }
    
    static public function getTopMenu(){
        $menu = new DaoMenu();
        $menus = array();
        $menus = $menu->getAll("parentID = '0' order by sortID ASC");
        return $menus;
    }
    
    static public function getSubMenu($parentID){
        $menu = new DaoMenu();
        $menus = array();
        $menus = $menu->getAll("parentID = '$parentID' order by sortID ASC");
        return $menus;
    }
    
    static public function hasSubMenu($parentID){
        $menu = new DaoMenu();
        $menus = array();
        $menus = $menu->getAll("parentID = '$parentID'");
        if(empty($menus)){
            return false;
        }else{
            return true;
        }
    }
	static public function getOne($menuID){
		$menuDao = new DaoMenu();
		$menu = $menuDao->getOne("id = '$menuID'");
		return $menu;
	}
	static public function getNameByID($menuID,$default=null){
		$menu = self::getOne($menuID);
		if(empty($menu)){
			return $default;
		}else{
			return $menu['title'];
		}
	}
	static public function getCurrent($typeID,$itemID=0){
		$menuDao = new DaoMenu();
		if($itemID==0){
			$where = " typeID='{$typeID}' ";
		}else{
			$where = " typeID='{$typeID}' and itemID='{$itemID}' ";
		}
		$menu = $menuDao->getOne($where);
		return $menu['id'];
	}
	static public function getTopParentID($menuID){
		$menu = self::getOne($menuID);
		if(!empty($menu)){
			$path = split(',',$menu['path']);
			return $path[1];
		}
		return 0;
	}
	static public function getUrlByID($menuID){
		$menu = self::getOne($menuID);
		switch($menu['typeID']){
			case service_type::PRODUCT:
				if($menu['itemID']==0){
					$url = url('product/index');
				}else{
					$url = service_uri::getCategoryUri($menu['itemID']);
				}
				break;
			case service_type::NEWS:
				if($menu['itemID']==0){
					$url = url('news/index');
				}else{
					$url = service_uri::getCategoryUri($menu['itemID']);
				}
				break;
			case service_type::PAGE:
				$url = service_uri::getPageUri($menu['itemID']);
				break;
			case service_type::ALBUM:
				$url = service_uri::getAlbumUri($menu['itemID']);
				break;
			case service_type::OTHER:
				$url = $menu['url'];
				break;
			case service_type::VIDEO:
				$url = url('video/index');
				break;
			case service_type::USER:
				$url = url('user/list');
				break;
			case service_type::NEWPRO:
				$url = url('newp/index');
				break;
		}
		return $url;
	}
}
?>
